Jittery Vertices problem (Appears to be a camera / matrix problem)

Started by
7 comments, last by Lord_Evil 15 years, 2 months ago
Hi all, EDIT: Ive added more relevant and important information to the bottom. Im working on a terrain renderer and ive just started adding some objects to the scene. Ive created a tower in blender and added it my scene. Ive not iced however from a distance it looks quite flickery and up close it appears as though the vertices of the object seem to be moving about slightly. It looks fairly horrible and Ive no idea why theyre moving. Ive checked my camera is actually moving slighty and that seems fine, ive tried removing translations and rotations, but they have no effect. Ive also tried disabling shaders and materials but the object still moves. This is all using OpenGL in C# using the Tao framework. Almost everything being rendered is using display lists including the problematic object. The terrain itself appears un affected but the water is. Im out of ideas for where to look for the cause of the problem? Has anyone seen this before? Any ideas how I might be able to fix it? Ive created a short video to show the problem here
">
Thanks for any help! EDIT: After some further investigation I think this is a problem with my camera and/or modelview matrix. Manually editing the matrix back to an identity matrix, completely removes the wobble/jitter effect. Ive tried checking through the camera rotation code to make sure everything is normalized, but the problem remains. The camera originally updated the modelview matrix every frame, regardless of any changes or not, ive stopped this, it now changes a few times after the input and then stops, this has no effect. The position of the obj file remains the same for every frame, so if I dont alter the camera, the camera and the obj should be in exactly the same position for every frame. The only other thing I thought of to try was to round the vertex coordinates for the obj to whole numbers, this still doesnt have an effect. Im completely out of ideas for trying to fix this, ive had the camera code around for sometime, its never really had much use though. This little project has got quite far for me but this problem is ruining everything. Here is the camera code in case any one can see anything obvious.


//
//  Camera
//
//  Camera
//

using Tao.OpenGl;

using System;
using System.Diagnostics;

using MyMathLib;


namespace Engine
{
    /// <summary>
    /// WalkCamera
    /// 
    /// A camera system designed to replicate a person walking through a scene.
    /// </summary>
    public class WalkCamera : ICamera
    {
        bool mAutoLevelX = true;

        public WalkCamera()
        {
            mCamPos = new Vector3_D();
            mCamX = new Vector3_D();
            mCamY = new Vector3_D();
            mCamZ = new Vector3_D();

            mMatrix = new Matrix_D();

            mCamRot = new Quaternion_D();
            mCamRot.make(0.0, 0.0, 1.0, 0.0);

        }

        //
        //  transform()
        //
        //  Transforms the camera rotation and position.
        //
        //  Rotates first then transforms along camera axis using transform inout vals.
        //
        public void transform(double theta, Vector3_D axis, Vector3_D transform)
        {
            Quaternion_D rotation = new Quaternion_D();
            rotation.make(MathUtil.degToRad(theta), axis[0], axis[1], axis[2]);
            rotation.normalize();

            Quaternion_D newQuaternion = new Quaternion_D();
            newQuaternion.mult(rotation, mCamRot);
            newQuaternion.normalize();

            mCamRot.copy(newQuaternion);
            mCamRot.normalize();

            newQuaternion.inverse();
            newQuaternion.normalize();
            newQuaternion.toMatrix(ref mMatrix);

            mCamX[0] = mMatrix[0];
            mCamX[1] = mMatrix[1];
            mCamX[2] = mMatrix[2];

            mCamY[0] = mMatrix[4];
            mCamY[1] = mMatrix[5];
            mCamY[2] = mMatrix[6];

            mCamZ[0] = mMatrix[8];
            mCamZ[1] = mMatrix[9];
            mCamZ[2] = mMatrix[10];

            Vector3_D vec = new Vector3_D();

            vec = mCamX.scale(transform[0]);
            mCamPos[0] += vec[0];
            mCamPos[1] += vec[1];
            mCamPos[2] += vec[2];

            vec = mCamY.scale(transform[1]);
            mCamPos[0] += vec[0];
            mCamPos[1] += vec[1];
            mCamPos[2] += vec[2];

            vec = mCamZ.scale(transform[2]);
            mCamPos[0] += vec[0];
            mCamPos[1] += vec[1];
            mCamPos[2] += vec[2];

            //if (mAutoLevelX)
            //    levelXAxis();
        }

        public void levelXAxis()
        {
            Vector3_D vecTrueUp = new Vector3_D();
            vecTrueUp[0] = 0.0;
            vecTrueUp[1] = 1.0;
            vecTrueUp[2] = 0.0;

            Vector3_D newXAxis;
            newXAxis = mCamZ.cross(vecTrueUp);

            float angle = (float)Math.Cos(newXAxis.dot(mCamX));
            Vector3_D rotAxis = newXAxis.cross(mCamX);

            Quaternion_D newQuat = new Quaternion_D();
            newQuat.make(angle, rotAxis[0], rotAxis[1], rotAxis[2]);

            Quaternion_D newCamQuat = new Quaternion_D();
            newCamQuat.mult(newQuat, mCamRot);

            mCamRot.copy(newCamQuat);
            mCamRot.normalize();

            newCamQuat.inverse();
            newCamQuat.normalize();
            newCamQuat.toMatrix(ref mMatrix);

            mCamX[0] = mMatrix[0];
            mCamX[1] = mMatrix[1];
            mCamX[2] = mMatrix[2];

            mCamY[0] = mMatrix[4];
            mCamY[1] = mMatrix[5];
            mCamY[2] = mMatrix[6];

            mCamZ[0] = mMatrix[8];
            mCamZ[1] = mMatrix[9];
            mCamZ[2] = mMatrix[10];
        }

        public void levelYAxis()
        {
            throw new NotImplementedException("WalkCamera::levelYAxis() Not implemented");
        }

        public void levelZAxis()
        {
            throw new NotImplementedException("WalkCamera::levelZAxis() Not implemented");
        }

        public void toOpenGLMatrix(ref double[] oglArray)
        {
            if (mRemainingUpdates > 0)
            {
                Matrix_D mtx = new Matrix_D();

                mtx.copy(mMatrix);
                mtx.transposeRot();

                Matrix_D mtxPos = new Matrix_D();
                mtxPos[12] = mCamPos[0];
                mtxPos[13] = mCamPos[1];
                mtxPos[14] = mCamPos[2];

                Matrix_D newMatrix = new Matrix_D();

                newMatrix.mult(mtx, mtxPos);

                for (int i = 0; i < 16; i++)
                {
                    oglArray = newMatrix;
                }
            }
        }

        public void applyCamera()
        {
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Glu.gluPerspective(30.0, 16.0f / 9.0f, 1.0f, 1500.0f);
            //Gl.glOrtho(0.0f, 100.0f, 0.0f, 100.0f, 1.0f, 5000.0f);

            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();


            double[] array = new double[16];

            toOpenGLMatrix(ref array);

            Gl.glMultMatrixd(array);
        }

        public void getCamZ(ref Vector3_D vec)
        {
            vec[0] = mCamZ[0];
            vec[1] = mCamZ[1];
            vec[2] = mCamZ[2];
        }

        public Vector3_D getCamX()
        {
            return mCamX;
        }
        public Vector3_D getCamY()
        {
            return mCamY;
        }
        public Vector3_D getCamZ()
        {
            return mCamZ;
        }

        public Vector3_D getCamPos()
        {
            return mCamPos;
        }

        public Matrix_D getMatrix()
        {
            return mMatrix;
        }

        public void setRemainingUpdates(int numUpdates)
        {
            mRemainingUpdates = numUpdates;
        }

        int mRemainingUpdates = 10;

        Vector3_D mCamPos;
        Vector3_D mCamX;
        Vector3_D mCamY;
        Vector3_D mCamZ;

        Matrix_D mMatrix;

        Quaternion_D mCamRot;
    }
}


[Edited by - ArThor on January 26, 2009 2:49:51 PM]
Advertisement
Did you check whether that still happens when your camera doesn't move?

Are the vertices located at some very high coordinates (like x = 1000.0f+)?
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
The vertices arent at high coordinates, no more than say 5 units from the origin.

Ive had a nother look at the camera, and if i manually adjust the values so the camera is pointing straight ahead with no tilt, the movement dissapears. From this ive gone back and tried to make sure everything is normalized at every step, but I cant seem to fix it. Any ideas?
Ive added some new information to the first post, Im still stuck trying to solve the problem however.
Quote:Original post by ArThor
The vertices arent at high coordinates, no more than say 5 units from the origin.

Ive had a nother look at the camera, and if i manually adjust the values so the camera is pointing straight ahead with no tilt, the movement dissapears. From this ive gone back and tried to make sure everything is normalized at every step, but I cant seem to fix it. Any ideas?


How small are the distances you're trying to represent? Make sure they aren't close (say within 1-2 orders of magnitude) to FLT_EPSILON.
Well you gotta think about what in your code changes. (relevant to drawing).

Print your matrices somewhere for a length of say 10 frames and examine them.
Maybe your accidently writing over some boundary and the variable your writing changes each frame.

At any rate, floating point numbers wether they are high values or not: A function takes one input and outputs another. So F(10000000.2) cannot have two output values otherwise computers make no sense. (assuming inputs dont change, but something has to be changing.)

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

I assume there's a floating point precision problem.

Try to check whether the modelview matrix and the projection matrix have changed during frames in which the jitter appears. You could do a read back from OpenGL directly before your draw call.

Generally, if the matrices and the vertex data don't change, no jitter should appear, as the calculations should have the same output for the very same input.

In your video the camera didn't seem to move but since you said the modelview matrix changed if you recalculated it even if nothing changed there might also be a problem with your push/pop usage.

Instead of Gl.glMultMatrixd(array) you could also use GL.glLoadMatrixd(array) which directly loads the matrix instead of multiplying with the identity.

Also, you multiply the camera rotation matrix with newly created quaternions/matrices multiple times, then call mCamRot.copy(...) which I guess sets new values for mCamRot.

Even if the matrices/quaternions are identity, each such operation might cause subtle changes to the values, especially if you alter the order.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Problem solved!

Ive had a quick look into things with your ideas, I quickly altered the obj model to draw a quad instead of the tower to check it wasnt the vertex coordinates I was sending and had the same problem. But I have a very similar test cube on the terrain with no wobble, so i forced the test cube to render the tower instead and got no wobble.

This quickly took me to the rendering order as the only difference between the two and the only thing in between them was the cloud system and the clouds move. Changing the order stopped the wobble.

Each cloud of about 150, is positioned using a

glTranslate(position),
draw,
glTranslate( -position ).

Ive replaced this with

glPushMatrix,
glTranslate(position),
draw
glPopMatrix

and the wobble goes away no matter what the rendering order is.

I assume the glTranslate calls are imprecise them selves, the position doesnt change during a frame, the final gl position after the clouds should return to 0,0,0.

Anyway, thanks for you help and ideas guys, I didnt think i was ever going to fix the problem.
Quote:Original post by ArThor
I assume the glTranslate calls are imprecise them selves, the position doesnt change during a frame, the final gl position after the clouds should return to 0,0,0.

It's not the glTranslate calls directly but floating point math on computers in general. There are always precision issues and if you call many arithmetic operations (like +p -p +p ...) even the smallest errors will add up.

If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

This topic is closed to new replies.

Advertisement